using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using Microsoft.Win32;
using System.Diagnostics;

namespace GMailNotifierNET.Classes.BrowserInformation
{
    public class BrowserFetcher
    {
        private ReadOnlyCollection<BrowserItem> m_browserItems;
        private BrowserItem m_defaultItem = null;

        public BrowserFetcher()
        {
            List<BrowserItem> browserItems = new List<BrowserItem>();
            string defaultBrowserKey = null;

            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                RegistryKey regDefaultBrowser = null;

                if (Environment.OSVersion.Version.Major == 5)   //Windows XP only supports maschine dependend default browser settings
                {
                     regDefaultBrowser = Registry.LocalMachine.OpenSubKey(
                        "Software\\Clients\\StartMenuInternet\\");
                }
                else if (Environment.OSVersion.Version.Major == 6) //Vista supports user-dependend default browser settings
                {
                    regDefaultBrowser = Registry.CurrentUser.OpenSubKey(
                       "Software\\Clients\\StartMenuInternet\\");
                }

                defaultBrowserKey = regDefaultBrowser.GetValue("").ToString();
            }

            
            
            RegistryKey regInternet = Registry.LocalMachine.OpenSubKey(
                "SOFTWARE\\Clients\\StartMenuInternet\\");

            foreach (string keyName in regInternet.GetSubKeyNames())
            {
                BrowserItem browserItem = new BrowserItem(regInternet.OpenSubKey(keyName));
                browserItems.Add(browserItem);

                if (keyName == defaultBrowserKey)
                    this.m_defaultItem = browserItem;
                    
            }

            this.m_browserItems = new ReadOnlyCollection<BrowserItem>(browserItems);           
        }

        public BrowserItem GetBrowserByBrowserKey(string browserKey)
        {
            foreach (BrowserItem item in this.m_browserItems)
            {
                if (item.BrowserKey == browserKey)
                    return item;
            }

            return this.m_defaultItem;
        }
        
        public ReadOnlyCollection<BrowserItem> BrowserItems
        {
            get { return this.m_browserItems; }
        }
        public BrowserItem DefaultBrowser
        {
            get { return this.m_defaultItem; }
        }

        public static void OpenWebsite(string browserPath, Uri url)
        {
            Process process =
                Process.Start(
                    new ProcessStartInfo(browserPath, url.ToString()));
        }
        public static void OpenWebsite(BrowserItem browser, Uri url)
        {
            BrowserFetcher.OpenWebsite(browser.Command, url);
        }
    }
}
